﻿ 
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using SHKF.B2C.MeiTuanShanGou.Model.Dto;
using SHKF.B2C.MeiTuanShanGou.Model.Push.Order;
using SkyApm.Tracing;
using SkyApm.Tracing.Segments;
using System; 

namespace SHKF.B2C.MeiTuanShanGou.Service.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        private readonly IEntrySegmentContextAccessor segContext;
        private readonly ILogger<TestController> logger;

        public TestController(IEntrySegmentContextAccessor segContext, ILogger<TestController> logger)
        {
            this.segContext = segContext;
            this.logger = logger;
        }
       
        public IActionResult Get()
        {
       
            string json = System.Text.Json.JsonSerializer.Serialize(segContext.Context);

            segContext.Context.Span.AddLog(LogEvent.Message($"Worker running test1: {DateTime.Now}"));
 
            return Ok($"TraceId={json}");
        }

        [HttpGet("Get2")]
        public string Get2()
        {
            return "Get2";
        }

        [HttpPost("CallBack")]
        public BaseDataDto CallBack(OrderPush order)
        {
            //try
            //{
            //    if (order != null)
            //    {
            //        if (order.app_poi_code > 0)
            //        {
            //            string json = JsonConvert.SerializeObject(order);

            //            this.segContext.Context.Span.AddLog(LogEvent.Message(json));

            //        }
            //    }
            //    else
            //    {
            //        this.logger.LogInformation("PushStatus>>order为空");
            //    }
            //}
            //catch (Exception ex)
            //{

            //}

            try
            {
                string json = JsonConvert.SerializeObject(order);

                segContext.Context.Span.AddLog(LogEvent.Message(json));
            }
            catch (Exception ex)
            { 
                throw ex;
            }
            

            BaseDataDto dto = new BaseDataDto();
            dto.data = "ok";
            return dto;
        } 
    }
}
